<template>
  <div style="text-align: center;" class="password-change">
    <h3>尊敬的 {{ this.$getSessionStorage("username") }} 用户,请修改你的密码。</h3>
    <el-form ref="passForm" :model="passForm" label-width="120px" :rules="rules">
      <el-form-item label="旧密码" prop="password">
        <el-input type="password" v-model="passForm.password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input type="password" v-model="passForm.newPass"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input type="password" v-model="passForm.checkPass"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>

export default {
  name: 'SetPwd',
  components: {},
  props: {},
  data() {
    //自定义验证规则，两次密码相同
    var validatePass=(rule,value,callback)=>{
      if(value==''){
        callback(new Error('请输入确认密码！'))
      }else if(value!==this.passForm.newPass){
        callback(new Error('两次输入的密码不一致！'))
      }else{
        callback();
      }
    }
    return {
      passForm: {
        password: '',
        newPass: '',
        checkPass: '',
      },
      rules: {
        password: [
          {required: true, message: '旧密码不能为空！', trigger: 'blur'},
          {min: 6, max: 12, message: '密码长度在6到12个字符！', trigger: 'blur'}
        ],
        newPass: [
          {required: true, message: '新密码不能为空！', trigger: 'blur'},
          {min: 6, max: 12, message: '密码长度在6到12个字符！', trigger: 'blur'}
        ],
        checkPass: [
          {required: true, validator:validatePass, trigger: 'blur'},
          {min: 6, max: 12, message: '密码长度在6到12个字符！', trigger: 'blur'}

        ]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    submitForm() {
      this.$refs.passForm.validate(valid => {
        if (valid) {
          this.$axios.post('/api/updatePass', this.passForm).then(response => {
            this.$message({
              message: response.data.message,
              type: 'success',
              showClose: true
            });
            // 密码修改成功以后，清空表单
            this.resetForm()
          })


        } else {
          return false;
        }
      });
    },
    resetForm() {

      this.passForm.password = ''
      this.passForm.newPass = ''
      this.passForm.checkPass = ''
    }
  },
  created() {
  },
  mounted() {
  }
};
</script>

<style scoped>


.password-change {
  /*background-image: url('');*/
  background-size: cover;
  background-position: center;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.el-form {
  margin: 0 auto;
  width: 400px;
  text-align: center;
}

/* 表单项样式 */
.el-form-item {
  margin-bottom: 20px;
}

/* 表单标签样式 */
.el-form-item__label {
  font-size: 16px;
  color: #666;
}

/* 输入框样式 */
.el-input__inner {
  border-radius: 20px;
  border-color: #dcdfe6;
  background-color: #f2f6fc;
  color: #606266;
}

/* 输入框聚焦时的样式 */
.el-input__inner:focus {
  border-color: #409EFF;
}

/* 按钮样式 */
.el-button {
  margin-top: 20px;
  margin-right: 20px;
  font-size: 14px;
  padding: 12px 20px;
  border-radius: 20px;
  background-color: #409EFF;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s;
}

/* 按钮悬停时的样式 */
.el-button:hover {
  background-color: #66b1ff;
}

/* 按钮点击时的样式 */
.el-button:active {
  background-color: #3a8ee6;
}

/* 错误提示样式 */
.el-form-item__error {
  margin-left: 5px;
  font-size: 12px;
  color: #f56c6c;
}
</style>